package com.silence.gmall.product.service.impl;

import com.silence.gmall.product.assembler.SpuImageFactory;
import com.silence.gmall.product.assembler.SpuInfoFactory;
import com.silence.gmall.product.assembler.SpuSaleAttrFactory;
import com.silence.gmall.product.assembler.SpuSaleAttrValueFactory;
import com.silence.gmall.product.domain.entity.SpuImage;
import com.silence.gmall.product.domain.entity.SpuInfo;
import com.silence.gmall.product.domain.entity.SpuSaleAttr;
import com.silence.gmall.product.domain.repository.SpuImageRepository;
import com.silence.gmall.product.domain.repository.SpuInfoRepository;
import com.silence.gmall.product.domain.repository.SpuSaleAttrRepository;
import com.silence.gmall.product.domain.repository.SpuSaleAttrValueRepository;
import com.silence.gmall.product.dto.SpuInfoDto;
import com.silence.gmall.product.dto.SpuSaleAttrDto;
import com.silence.gmall.product.service.SpuInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @Author silence
 * @Description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @Date 2025-08-17 16:28:16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SpuInfoServiceImpl implements SpuInfoService {

    /**
     * SpuInfoRepository 的注入
     */
    private final SpuInfoRepository spuInfoRepository;

    /**
     * SpuImageRepository 的注入
     */
    private final SpuImageRepository spuImageRepository;

    /**
     * SpuSaleAttrRepository 的注入
     */
    private final SpuSaleAttrRepository spuSaleAttrRepository;

    /**
     * SpuSaleAttrValueRepository 的注入
     */
    private final SpuSaleAttrValueRepository spuSaleAttrValueRepository;

    /**
     * SpuInfoFactory 的注入
     */
    private final SpuInfoFactory spuInfoFactory;

    /**
     * SpuImageFactory 的注入
     */
    private final SpuImageFactory spuImageFactory;

    /**
     * SpuSaleAttrFactory 的注入
     */
    private final SpuSaleAttrFactory spuSaleAttrFactory;

    /**
     * SpuSaleAttrValueFactory 的注入
     */
    private final SpuSaleAttrValueFactory spuSaleAttrValueFactory;

    /**
     * 添加spu
     *
     * @param reqVo spu信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfoDto reqVo) {
        // 保存 SPU 信息
        SpuInfo spuInfo = spuInfoFactory.saveSpuInfoReqVo2SpuInfo(reqVo);
        spuInfoRepository.save(spuInfo);

        // 获取 SPU ID
        Long spuId = spuInfo.getId();

        // 保存商品图片集合
        if (CollectionUtils.isNotEmpty(reqVo.getSpuImageList())) {
            List<SpuImage> spuImageList = spuImageFactory
                    .spuImageDtoList2SpuImageListWithContext(reqVo.getSpuImageList(), spuId);
            spuImageRepository.saveBatch(spuImageList, spuImageList.size());
        }

        // 保存销售属性
        if (CollectionUtils.isNotEmpty(reqVo.getSpuSaleAttrList())) {
            List<SpuSaleAttrDto> spuSaleAttrDtoList = reqVo.getSpuSaleAttrList();

            // 转换并保存销售属性
            List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrFactory
                    .spuSaleAttrDtoList2SpuSaleAttrListWithContext(spuSaleAttrDtoList, spuId);
            spuSaleAttrRepository.saveBatch(spuSaleAttrList, spuSaleAttrList.size());

            // 批量保存销售属性值
            Optional.of(spuSaleAttrDtoList.stream()
                            .filter(attr -> CollectionUtils.isNotEmpty(attr.getSpuSaleAttrValueList()))
                            .flatMap(attr -> spuSaleAttrValueFactory
                                    .spuSaleAttrValueDtoList2SpuSaleAttrValueListWithContext(attr.getSpuSaleAttrValueList(), spuId, attr.getSaleAttrName())
                                    .stream())
                            .toList())
                    .filter(CollectionUtils::isNotEmpty)
                    .ifPresent(list -> spuSaleAttrValueRepository.saveBatch(list, list.size()));
        }
    }
}